package com.buildings.util;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.inputmethod.InputMethodManager;

import com.buildings.activity.LoginActivity;
import com.buildings.base.BaseFormActivity;
import com.buildings.permissions.Acp;
import com.buildings.permissions.AcpListener;
import com.buildings.permissions.AcpOptions;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.util.List;

public class Utils {

    //此方法只是关闭软键盘
    public static void hintKb(Activity context) {
        InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm.isActive() && context.getCurrentFocus() != null) {
            if (context.getCurrentFocus().getWindowToken() != null) {
                imm.hideSoftInputFromWindow(context.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
            }
        }
    }

    /**
     * 将px值转换为sp值，保证文字大小不变
     *
     * @param pxValue
     * @param pxValue d
     *            （DisplayMetrics类中属性scaledDensity）
     * @return
     */
    public static int px2sp(Context context, float pxValue) {
        final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
        return (int) (pxValue / fontScale + 0.5f);
    }

    /**
     * 将sp值转换为px值，保证文字大小不变
     *
     * @param spValue
     * @param spValue
     *            （DisplayMetrics类中属性scaledDensity）
     * @return
     */
    public static int sp2px(Context context, float spValue) {
        final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
        return (int) (spValue * fontScale + 0.5f);
    }

    public static void logout(Activity activity){
        SPUtil.clear();
        Intent intent = new Intent(activity,LoginActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        activity.startActivity(intent);
    }


    /**
     * 权限访问
     */
    public static void askMultiPermission(Context context, final BaseFormActivity.SelectPermissionCallBack callBack, String... permission) {
        Acp.getInstance(context).
                request(new AcpOptions.Builder()
                                .setPermissions(permission)

                                /*以下为自定义提示语、按钮文字
                                .setDeniedMessage()
                                .setDeniedCloseBtn()
                                .setDeniedSettingBtn()
                                .setRationalMessage()
                                .setRationalBtn()*/

                                .build(),
                        new AcpListener() {
                            @Override
                            public void onGranted() {
                                //getIMEI();
                                callBack.onGranted();
                            }

                            @Override
                            public void onDenied(List<String> permissions) {
                            }
                        });
    }


    public static String savePictureBitmap(String localPath){
        String mFileName = System.currentTimeMillis() + ".png";
        Bitmap bitmap = BitmapFactory.decodeFile(localPath);
        File f = new File(FileUtils.getPhotoCacheDir(), mFileName);
        FileOutputStream stream = null;
//        try {
//            stream = new FileOutputStream(f);
//        } catch (FileNotFoundException e) {
//            e.printStackTrace();
//        }
//        if (stream != null) {
//            bitmap.compress(Bitmap.CompressFormat.PNG, 20, stream);// (0
//            // -
//        }
        // 0-100 100为不压缩
        int quality = 70;
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        // 把压缩后的数据存放到baos中
        bitmap.compress(Bitmap.CompressFormat.JPEG, quality, baos);
        try {
            FileOutputStream fos = new FileOutputStream(f);
            fos.write(baos.toByteArray());
            fos.flush();
            fos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return f.getPath();
    }
}
